/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-13 17:59:48
 * @Last modified by:   ls-mac
 * @Last modified time: 2020-03-08 23:35:01
 */
import { packageAddOrEdit } from 'services/package';
import { orderPage } from 'services/order';
import { reportPage, reportDelete } from 'services/report';

export default {
  namespace: 'orderReport',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(reportPage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },

    *packageAddOrEdit({ payload, callback }, { call, put }) {
      const response = yield call(packageAddOrEdit, payload);
      callback(response);
    },

    *reportDelete({ payload, callback }, { call, put }) {
      const response = yield call(reportDelete, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { reportList, pageIndex, pageSize, recordCount },
      } = action.payload;

      if (!(reportList instanceof Array)) {
        return;
      }

      const newList = reportList.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });

      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
  },
};
